package com.bestvike.stone.spring.redis.converter;

import com.bestvike.stone.core.collection.ArrayUtils;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * Created by 许崇雷 on 2018/5/30.
 */
public final class LongRedisSerializer implements RedisSerializer<Long> {
    public static final LongRedisSerializer DEFAULT = new LongRedisSerializer();

    @Override
    public byte[] serialize(Long value) throws SerializationException {
        return value == null ? ArrayUtils.EMPTY_BYTE_ARRAY : value.toString().getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public Long deserialize(byte[] bytes) throws SerializationException {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.put(bytes);
        return buffer.getLong();
    }
}
